var app = getApp(), fetchApi = require("../../../utils/httpPost.js"), i = require("../../../utils/arrayUtil.js")
Page({
  /**
   * 页面的初始数据
   */
  data: {
    imgurl: app.getImgUrl(),
    searchValue: "",
    searchList: [],
    searchResult: !1,
    hasConcent: !0,
    hotWordList: [],
    historyArray: [],
    typeList:[],
    productList:[],
    pageIndex: 1,
    pageSize: 10,
    showLoading: false,
    searchContent: "",
    keyWordList: [],
    isEnd: !1,
    splitstart: 0,
    isLoading:true,
    loading: false,
    netWork: true,
    lastpg: false,
    lastList: [],
    defaultWord: "搜索您需要的商品",
    isiOs: !0,
    currentTab:0,
    jd:0,
    wd:0,
    type_id:0,
  },
  modalcnt:function  (keyword)  {
    var that = this
    that.data.pageIndex = 1
    //获取经纬度  
    wx.getLocation({
      type:  'gcj02',
      success:  function  (res)  {
      console.log('modalcnt', res)
      var latitude = res.latitude
      var longitude = res.longitude
      that.data.wd = latitude//纬度
      that.data.jd = longitude//经度
      app.setStorageSync("lng",longitude);
      app.setStorageSync("lat",latitude);
      that.onHttpInit();
    },fail:function(res){
        that.data.wd = 0
        that.data.jd = 0
        that.onHttpInit()
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var d = this;
    wx.getStorage({
      key: "historySearch",
      success: function (a) {
        console.log("historySearch:", a), 
        d.setData({ 
          historyArray: a.data  
        });
      }
    })
    this.modalcnt();
  },
  handleChange({ detail }) {
    this.data.currentTab = detail.index;
    this.data.pageIndex = 1;
    this.data.type_id = this.data.typeList[detail.index].id;
    this.data.productList = []
    this.setData({
      isLoading:true,
      productList:[],
    })
    this.onHttpInit()
  },
  onHttpInit: function () {
    var that = this
    fetchApi.httpPost("/product/listBg", {
      type_id: that.data.type_id,
      keyword: that.data.searchValue,
      pageIndex: that.data.pageIndex,
      pageSize: that.data.pageSize,
    }, function (res) {
      console.log("获取商品列表:", res)
      var productList = res.data.data.products;
      that.data.type_id = res.data.data.typeList[0].id;
      for (var index in productList) {
        that.data.productList.splice(that.data.productList.length, 0, productList[index]);
      }
      that.setData({ 
        typeList: res.data.data.typeList,
        searchResult:true,
        hasConcent:true,
        productList: that.data.productList,
        isLoading: false
      });
      wx.stopPullDownRefresh(); //停止下拉刷新
    }, function (a) {
      console.log("keyWork err:", a);
    });
  },
  goBackPreRoute: function () {
    wx.navigateBack({ delta: 1 });
  },
  pushToAd: function (a) {
    d.pushToAd(a, "search");
  },
  pushToDetail_2: function (t) {
    console.log("进入详情页", t);
    var d = t.detail.target.dataset.item.id, s = t.detail.formId;
    var i = "/pages/product/detail/index?id=" + d + "&noSku=noSku&_from=appSearch&param1=" + this.data.searchValue;
    wx.navigateTo({ url: i });
  },
  bindClearHistory: function () {
    var that = this;
    wx.removeStorage({
      key: "historySearch",
      success: function (t) {
        console.log("清空历史搜索：", t),
          that.setData({
            historyArray: []
          });
      }
    });
  },
  saveHistorySearch: function (a) {
    var that = this, e = this.data.historyArray;
    e.map(function (t, d) {
      t == a && e.splice(d, 1);
    }), e.push(a),
      wx.setStorage({
        key: "historySearch",
        data: e,
        success: function () {
          that.setData({
            historyArray: e
          });
        }
      });
  },
  searchHisItem: function (a) {
    console.log("点击hisitem:", a.currentTarget.dataset.hisitem), this.setData({
      searchValue: a.currentTarget.dataset.hisitem,
      pageIndex: 1,
      searchList: [],
      splitstart: 0,
      lastList: [],
      isLoading: !0
    }), this.fetchSearchData(a.currentTarget.dataset.hisitem, 1);
  },
  bindSearch: function () {
    this.data.pageIndex = 1;
    this.data.productList = [];
    this.setData({
      pageIndex: 1,
      productList: [],
    })
    if(this.data.searchValue.length > 0){
      console.log("调用搜索"), 
      this.setData({ isLoading: !0 })
      this.modalcnt(this.data.searchValue);
    }else{
      wx.showModal({
        title: "提示",
        content: "请输入搜索内容",
        showCancel: !1
      });
    }
  },
  onChange(e){
    console.log('onChange',e)
    this.setData({
      searchValue: e.detail
    });
  },
  onSearch(val){
    console.log('onSearch',val)
    this.data.pageIndex = 1;
    this.data.productList = [];
    this.setData({
      productList: [],
    })
    this.onHttpInit()
  },
  onReachBottom: function () {
    console.log("上啦加载")
    if(this.data.lastpg){
      this.setData({
        showLoading: !1,
        isEnd: !0
      })
    }else{
      this.setData({
        showLoading: !0,
        isEnd: !1
      })
    };
    if(!this.data.searchValue && !this.data.searchContent){
      this.onHttpInit()
    }
  }
})